<?php
session_start();
require_once("konf.php");
if (!isset($_SESSION['CREATED'])) {
    $_SESSION['CREATED'] = time();
} else if (time() - $_SESSION['CREATED'] > 36000) {
	$kask=$yhendus->prepare("DELETE FROM  broneering WHERE broneering_id=? and klient_id IS NULL");
	$kask->bind_param("i", $_SESSION["broneerin"]);
	$kask->execute();

	$kask=$yhendus->prepare("DELETE FROM  elamine WHERE broneering_id=? and klient_id IS NULL");
	$kask->bind_param("i", $_SESSION["broneerin"]);
	$kask->execute();
    // session started more than 30 minutes ago
    { session_destroy(); header("Location: registreerimine.php"); 
}
if(!isSet($_SESSION["broneerin"])){
  header("Location: registreerimine.php");
 exit();
} } 


if(!empty($_REQUEST["uusleht"])){
		$kask=$yhendus->prepare(
	   "UPDATE broneering SET algusaeg=?, lopuaeg=? WHERE broneering_id=?");
        echo $yhendus->error;
        $kask->bind_param("ssi",$_REQUEST["datepicker"],$_REQUEST["datepicker2"], $_SESSION["broneerin"]);
        $kask->execute();
         $_SESSION["algusaeg"]=$_REQUEST["datepicker"];
         $_SESSION["lopuaeg"]=$_REQUEST["datepicker2"];
	}
     
if(isSet($_REQUEST["vali"])){
    $kask=$yhendus->prepare("INSERT INTO elamine (broneering_id, ruumiliigid_id, kogus, algusaeg, lopuaeg) VALUES (?,?, 1, ?, ?)");
    $kask->bind_param("iiss", $_SESSION["broneerin"], $_REQUEST["ruumiliigid_id"], $_SESSION["algusaeg"], $_SESSION["lopuaeg"] );
     
    $kask->execute();
     //$_SESSION["broneerin"]=$yhendus->insert_id; on juba olemas sessionis!!!
      header("Location: toad.php");
	$_SESSION["ruumiliik"]=$_REQUEST["ruumiliigid_id"];
	$_SESSION["kogus"]=$_REQUEST["kogus"];exit();
}
             


?>
<?php 
 
if(isSet($_REQUEST["kustutusid"])){
 $kask=$yhendus->prepare("DELETE FROM elamine WHERE elamine_id=?");
 $kask->bind_param("i", $_REQUEST["kustutusid"]);
 $kask->execute();
 }
?>



<!doctype html>
<html>
        <head>
                <title>Toa valik</title>
				<link rel="stylesheet" type="text/css" href="kujundus.css"/>
								<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css">
  <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
  <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>


  <script>
  $(function() {
        var date = $('#datepicker').datepicker({ dateFormat: 'yy-mm-dd' }).val();
  });
  </script>
  <script>
  $(function() {
        var date = $('#datepicker2').datepicker({ dateFormat: 'yy-mm-dd' }).val();
  });
  </script>
				 
        </head>
		<header>
  <div id="firma">
  <h1>IFI Grand Hotell</h1>
  </div>
  
  </header>
<body>
<section class="menyy">
<ul style="color:white; text-shadow:0px 0px 5px rgba(255,255,255, 1);"> 1. Valige oma toad</ul>
<ul >2. Esitage oma andmed</ul>
<ul>3. Tehtud</ul>

</section>

<section id="keha2">
<section class="keha2">
        <form action="?">
        <input type="hidden" name="uusleht" value="$id" />
        <td><p>Saabumine <input type="text" id="datepicker" name="datepicker" value="<?php echo $_SESSION['algusaeg'];?>"></td>
        <td>Lahkumine <input type="text" id="datepicker2" name="datepicker2" value="<?php echo $_SESSION['lopuaeg'];?>"></td>
                <td><input  type="submit" value="Muuda" /></td>
                </form>
				</p>
</section>

                <h2>Vali tuba</h2>


<table border="1px">
<thead>
                <tr>
               
                
				<th>Ruumiliik</th>
                <th>Vabu ruume</th> 
				<th>Hind</th>
                <th>Kohti</th>
<th>Kinnita valik</th>

                </thead>


<tbody>
        <?php
        $kask=$yhendus->prepare("SELECT ruum.ruumiliigid_id, ruumiliigid.liik, hind, kohtadearv,  COUNT(*)-
 (SELECT COUNT(*) FROM elamine, broneering WHERE broneering.broneering_id=? and ((elamine.algusaeg 
between broneering.algusaeg and broneering.lopuaeg) or (elamine.lopuaeg 
between broneering.algusaeg and broneering.lopuaeg)) AND elamine.ruumiliigid_id=ruum.ruumiliigid_id)
   as vabu
FROM ruum, ruumiliigid, hinnad WHERE ruum.ruumiliigid_id=ruumiliigid.ruumiliigid_id and ruumiliigid.hinnad_id=hinnad.hinnad_id GROUP BY ruumiliigid_id;");
 $kask->bind_param("i", $_SESSION["broneerin"]);
 $kask->bind_result($ruumiliik, $liiginimi, $hind, $kohti, $kogus2);
$kask->execute();

   while($kask->fetch()){if($kogus2>0){
     echo "
<form action='?'>
    <tr>
    <input type='hidden' name='ruumiliigid_id' value='$ruumiliik' />
    
	<td>$liiginimi </td>
    <td>$kogus2</td>
	<td>$hind eurot</td>
	<td>$kohti</td>
<input type='hidden' name='vali' value='valitud'/>
    <td><input type='submit' value='Vali'/></td>
     </td>
 </form>
    </tr>
    ";
                
     }}?> 
 </tbody>
  <table>
  <p></p>

<a class="link" href='klient.php'>edasi</a>
  
  
<h2>Valitud toad</h2>
  
<table border="1">
   <colgroup>
        <col span="1">
        <col span="1">
   </colgroup>
<thead> 
        <tr>
		
                <td colspan="1">Ruumiliik</td>
                <td colspan="1">Saabumine</td>
                <td colspan="1">Lahkumine</td>
                <td colspan="1">Hind</td>
				<td colspan="1">Hind kokku</td>
                <td colspan="1">Kohtade arv</td>
                
        </tr>
   </thead>
   </thead>
   <?php
 $kask=$yhendus->prepare("SELECT elamine.elamine_id, ruumiliigid.liik, elamine.algusaeg, elamine.lopuaeg, hinnad.hind, kohtadearv FROM elamine, broneering, ruumiliigid, hinnad 
   WHERE broneering.broneering_id=? and broneering.broneering_id=elamine.broneering_id and ruumiliigid.hinnad_id=hinnad.hinnad_id 
   and elamine.ruumiliigid_id=ruumiliigid.ruumiliigid_id; ");
   $kask->bind_param("i", $_SESSION["broneerin"]);
  $kask->bind_result ($id, $liik, $algusaeg, $lopuaeg, $hind, $kohtadearv);
  $kask->execute();

 date_default_timezone_set('Europe/Helsinki');
	while($kask->fetch()){
	$start=strtotime($algusaeg);
	$end=strtotime($lopuaeg);
	$datediff = ceil(abs($end - $start) / 86400);
	$hindkokku=($hind*$datediff);
                  echo "
                    <tr>
				
                        <td>$liik</td>
                        <td>$algusaeg</td>
                        <td>$lopuaeg</td>
                        <td>$hind €</td>
						<td>$hindkokku €</td>
                        <td>$kohtadearv</td>
						<td><a href='?kustutusid=$id'>kustuta</a></td>
                          </td>
                        </tr>
                  ";
                }
          ?>
        </table>





</section>
</section>
</section>

</body>
</html>